package cn.lf.android.lawfirm.uploadcontroller;

import android.content.Context;
import android.util.Pair;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.downloadcontroller.modle.Agreement;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.undertake.apply.modle.FileInfo;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * Created by qianfan on 2017/8/31.
 */

public class UpLodeController extends BaseController {
    UpLodeListCallback mUpLodeListCallback;
    private Context context;

    public UpLodeController(UpLodeListCallback callback, Context context) {
        this.mUpLodeListCallback = callback;
        this.context = context;
    }


    /**
     * //"/storage/emulated/0/ebilling/imgs/20170914140546242.jpg"
     *
     * @param caseid
     * @param filetype
     * @param path
     */
    public void onUpLode(String caseid, String filetype, String path) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseid", caseid);
        params.put("filetype", filetype);
        String dd = path.replace("file://", "");
        List<Pair<String, File>> list = new ArrayList<Pair<String, File>>();
        Pair<String, File> pair = new Pair<String, File>("files", new File(path));
        list.add(pair);
        DebugLog.e("path==" + path);
        DebugLog.e("caseid==" + caseid);
        DebugLog.e("filetype==" + filetype);
        DebugLog.e("list==" + list);
        new MyOkHttpClient().uploadFiles(AppConfig.Apply_UploadFile, params, list,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("上传文件回调===" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<Agreement> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<Agreement>>() {
                                                });
                                mUpLodeListCallback.onUpLodeListuccessed(beans);
                            } else {
                                mUpLodeListCallback
                                        .onUpLodeListFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("上传文件失败回调6==" + errorMsg);
                        mUpLodeListCallback.onUpLodeListFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface UpLodeListCallback {
        public void onUpLodeListuccessed(List<Agreement> beans);

        public void onUpLodeListFailed();

    }
}
